package com.xiaohao.student_course.service;

import com.xiaohao.student_course.bean.Student;
import com.xiaohao.student_course.bean.StudentQueryParam;
import com.xiaohao.student_course.dao.StudentDao;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.domain.*;
import org.springframework.stereotype.Service;

/**
 * Created with IntelliJ IDEA.
 *
 * @author 小浩
 * @date 2018-12-16 08:44
 * @package com.xiaohao.student_course.service
 * @description
 */
@Service
public class StudentService {
    @Autowired
    private StudentDao studentDao;
    public Page<Student> studentQuery(StudentQueryParam studentQueryParam){
        PageRequest of = PageRequest.of(studentQueryParam.getPageNumber(),studentQueryParam.getPageSize(),new Sort(Sort.Direction.ASC, "id"));
//        Page<Student> all = studentDao.findAll(new Specification<Student>() {
//            @Override
//            public Predicate toPredicate(Root<Student> root, CriteriaQuery<?> criteriaQuery, CriteriaBuilder criteriaBuilder) {
//                Predicate majorName = null,classesName = null,studentID = null;
//                if (StringUtils.isEmpty(studentQueryParam.getMajorName())){
//                    majorName= criteriaBuilder.and(criteriaBuilder.equal(root.get("majorName"), studentQueryParam.getMajorName()));
//                }
//                if (StringUtils.isEmpty(studentQueryParam.getClassesName())){
//                    classesName = criteriaBuilder.and(criteriaBuilder.equal(root.get("classesName"), studentQueryParam.getClassesName()));
//
//                }
//                if (StringUtils.isEmpty(studentQueryParam.getStudentID())){
//                    studentID = criteriaBuilder.and(criteriaBuilder.equal(root.get("studentID"), studentQueryParam.getStudentID()));
//                }
//                return criteriaBuilder.and(majorName,classesName,studentID);
//            }
//        },of);

        if (studentQueryParam.getStudent()==null){
            return studentDao.findAll(of);
        }
        Example<Student> example=Example.of(studentQueryParam.getStudent(),ExampleMatcher.matching().withIgnoreNullValues());

        return studentDao.findAll(example,of);
    }
}
